//
//  PushSecond.m
//  HYHOME
//
//  Created by Mac on 16/2/15.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "PushSecond.h"
#import "Head.h"
#import "PushSecondCell.h"
#import "NetWorkHandler.h"
#import "SpeakModel.h"
#import "UIImageView+WebCache.h"
#import "DKNightVersion.h"
#import "PushSecondTwo.h"
@interface PushSecond ()<UITableViewDataSource,UITableViewDelegate,NetWorkHandlerDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic,retain)NSMutableArray *arr;

@end

@implementation PushSecond

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    [self createtableView];
    [self handle];
    
}

- (void)handle{

    NSString *str = [NSString stringWithFormat:@"http://api.wsq.umeng.com/v2/feed/?os=iOS&openudid=c4fef44ea2c33987b846791b59bd050a622bea9b&sdkv=2.2.1&ak=5636cff167e58ef91b0001ae&de=iPhone5,2&mac=02:00:00:00:00:00&feed_id=%@",self.idNumber];

    [NetWorkHandler handlerJSONWithURL:str delegate:self];
    

}


- (void)haneldDidComplete:(id)result{

    
    self.array = [NSMutableArray array];
    self.arr = [NSMutableArray array];
    
    NSDictionary *dicC = [result objectForKey:@"creator"];
    
    SpeakModel *model = [[SpeakModel alloc]init];
    
    model.name = [dicC objectForKey:@"name"];
    
    NSDictionary *dicI = [dicC objectForKey:@"icon_url"];
    
    model.icon_url = [dicI valueForKey:@"240"];
    
    
    //文字 text
    model.content = [result objectForKey:@"content"];
    
    [self.array addObject:model];
    
    NSArray *array = [result objectForKey:@"image_urls"];
    for (NSDictionary *dicaa in array) {
        
        SpeakModel *model2 =[[SpeakModel alloc]init];
        model2.pic = [dicaa objectForKey:@"360"];
        
        [self.arr addObject:model2];
    }
    
    

    
    
    
    [self.tableView reloadData];
    


}
- (void)createtableView{

    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    // 头部
    [self.tableView registerClass:[Head class] forHeaderFooterViewReuseIdentifier:@"pool"];
    
    [self.tableView registerClass:[PushSecondCell class] forCellReuseIdentifier:@"pool2"];
    [self.tableView registerClass:[PushSecondTwo class] forCellReuseIdentifier:@"pool3"];

    
    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
    self.tableView.dk_separatorColorPicker = DKColorWithRGB(0xaaaaaa, 0x313131);
    self.tableView.showsVerticalScrollIndicator = NO;


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.arr.count ;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    Head *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"pool"];
  
    SpeakModel *model = [self.array objectAtIndex:section];
    
    [header passModel:model];
    
    return header;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        
        
        PushSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
    
        
        cell.selectionStyle = UITableViewCellAccessoryNone;

        SpeakModel *model = [self.array objectAtIndex:indexPath.row];
        
        [cell passModel:model];
        
        return cell;
    }else {
    
        PushSecondTwo *cell2 = [tableView dequeueReusableCellWithIdentifier:@"pool3"];
        SpeakModel *model2 = [self.arr objectAtIndex:indexPath.row -1];
        
        cell2.selectionStyle = UITableViewCellAccessoryNone;

        [cell2 passModel:model2];
        return cell2;
    
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        return [PushSecondCell heightForCell:self.array[indexPath.row]];
    }else{
    
        return 210;
    
    }
 
    


}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return [Head heightForCell:self.array[section]];
}






- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
